datetime purity. (#1102)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Tue, 9 May 2023 13:12:32 +0000 (07:12 -0600)
committerGitHub <noreply@github.com>
Tue, 9 May 2023 13:12:32 +0000 (07:12 -0600)
eliminate some QDateTime -> gpsbabel:DateTime conversions.  These
can also help performance as DateTime defaults to UTC.

eliminate some unecessary involvment of the UNIX epoch when computing
time differences.

gpx.cc
interpolate.cc
interpolate.h
kml.cc
position.cc
position.h

diff --git a/gpx.cc b/gpx.cc
index 246e709afe6ba774ada52e8ffd7010b2c422d158..7b499a7d6cc26609cdf1b717aa2ee4d4c2d9ddea 100644 (file)
--- a/gpx.cc
+++ b/gpx.cc
@@ -437,7 +437,7 @@ xml_parse_time(const QString& dateTimeString)
   }
 
   int year = 0, mon = 0, mday = 0, hour = 0, min = 0, sec = 0;
-  QDateTime dt;
+  gpsbabel::DateTime dt;
   int res = sscanf(timestr, "%d-%d-%dT%d:%d:%d", &year, &mon, &mday, &hour,
                    &min, &sec);
   if (res > 0) {
@@ -452,8 +452,6 @@ xml_parse_time(const QString& dateTimeString)
 
     // Any offsets that were stuck at the end.
     dt = dt.addSecs(-off_sign * off_hr * 3600 - off_sign * off_min * 60);
-  } else {
-    dt = QDateTime();
   }
   return dt;
 }
@@ -461,7 +459,7 @@ xml_parse_time(const QString& dateTimeString)
 void
 GpxFormat::gpx_end(QStringView /*unused*/)
 {
-  static QDateTime gc_log_date;
+  static gpsbabel::DateTime gc_log_date;
 
   // Remove leading, trailing whitespace.
   cdatastr = cdatastr.trimmed();
@@ -568,7 +566,7 @@ GpxFormat::gpx_end(QStringView /*unused*/)
         (!wpt_tmp->gc_data->last_found.isValid())) {
       wpt_tmp->AllocGCData()->last_found = gc_log_date;
     }
-    gc_log_date = QDateTime();
+    gc_log_date = gpsbabel::DateTime();
     break;
   case tt_cache_favorite_points:
     wpt_tmp->AllocGCData()->favorite_points  = cdatastr.toInt();
index f61ff6fab6a12792f4b6d1eeedcc38924df89fd2..09fcebc54bb663c66984d9b05c94351df8cf3a12 100644 (file)
 
  */
 
+#include "interpolate.h"
+
 #include <climits>              // for INT_MAX
 #include <cmath>                // for abs, ceil, isfinite, round
 #include <cstdlib>              // for abs, strtod
 #include <optional>             // for optional
 
 #include <QString>              // for QString
-#include <QtGlobal>             // for qAsConst, QAddConst<>::Type
+#include <QtGlobal>             // for qint64, qAsConst, qRound64
 
 #include "defs.h"
-#include "interpolate.h"
+#include "formspec.h"           // for FormatSpecificDataList
 #include "grtcirc.h"            // for linepart, RAD, gcdist, radtomiles
 #include "src/core/datetime.h"  // for DateTime
 #include "src/core/logging.h"   // for Fatal
@@ -81,8 +83,7 @@ void InterpolateFilter::process()
       } else {
         std::optional<qint64> timespan;
         if (wpt->creation_time.isValid() && time1.isValid()) {
-          timespan = wpt->creation_time.toMSecsSinceEpoch() -
-                     time1.toMSecsSinceEpoch();
+          timespan = time1.msecsTo(wpt->creation_time);
         }
         std::optional<double> altspan;
         if (altitude1 != unknown_alt && wpt->altitude != unknown_alt) {
@@ -121,8 +122,7 @@ void InterpolateFilter::process()
           wpt_new->shortname = QString();
           wpt_new->description = QString();
           if (timespan.has_value()) {
-            wpt_new->SetCreationTime(0, time1.toMSecsSinceEpoch() +
-                                     round(frac * *timespan));
+            wpt_new->SetCreationTime(time1.addMSecs(qRound64(frac * *timespan)));
           } else {
             wpt_new->creation_time = gpsbabel::DateTime();
           }
@@ -152,7 +152,7 @@ void InterpolateFilter::process()
       lat1 = wpt->latitude;
       lon1 = wpt->longitude;
       altitude1 = wpt->altitude;
-      time1 = wpt->creation_time;
+      time1 = wpt->creation_time.toUTC();  // use utc to avoid tz conversions.
     }
   }
   backuproute.flush();
index 5aabe1aa258c857a34e42318c115618c15444a50..5df73a8eaa79528a5501c60eb2267264587c135d 100644 (file)
 #ifndef INTERPOLATE_H_INCLUDED_
 #define INTERPOLATE_H_INCLUDED_
 
-#include <optional>             // for optional
-
+#include <QString>              // for QString
 #include <QVector>              // for QVector
-#include <QtGlobal>             // for qint64
 
 #include "defs.h"               // for ARG_NOMINMAX, arglist_t, ARGTYPE_BEGIN_EXCL, ARG...
 #include "filter.h"             // for Filter
diff --git a/kml.cc b/kml.cc
index 8683887acaaca6569aa3b590d26b77209c38fcde..f05d86aa21741974dbf4e66fd82380c52b42528d 100644 (file)
--- a/kml.cc
+++ b/kml.cc
@@ -354,8 +354,8 @@ void KmlFormat::wr_init(const QString& fname)
 {
   char u = 's';
   waypt_init_bounds(&kml_bounds);
-  kml_time_min = QDateTime();
-  kml_time_max = QDateTime();
+  kml_time_min = gpsbabel::DateTime();
+  kml_time_max = gpsbabel::DateTime();
 
   if (opt_units) {
     u = tolower(opt_units[0]);
index 4d2d0695d9ec78f242066c680848e6066dc8ffec..b99f55de3fd6e7ec8a08c4be6ea1046afe92bbe9 100644 (file)
 
  */
 
-#include <cmath>            // for fabs
-#include <cstdlib>          // for strtod
+#include "position.h"
+
+#include <cmath>                // for abs
+#include <cstdlib>              // for strtod
 
-#include <QList>            // for QList
-#include <QtGlobal>         // for qAsConst, QAddConst<>::Type
+#include <QList>                // for QList
+#include <QtGlobal>             // for qAsConst, qRound64, qint64
 
 #include "defs.h"
-#include "grtcirc.h"        // for RAD, gcdist, radtometers
-#include "position.h"
+#include "grtcirc.h"            // for RAD, gcdist, radtometers
+#include "src/core/datetime.h"  // for DateTime
 
 #if FILTERS_ENABLED
 
@@ -64,7 +66,7 @@ void PositionFilter::position_runqueue(WaypointList* waypt_list, int qtype)
 
           if (dist <= pos_dist) {
             if (check_time) {
-              double diff_time = fabs(waypt_time(qlist.at(i).wpt) - waypt_time(qlist.at(j).wpt));
+              qint64 diff_time = std::abs(qlist.at(j).wpt->creation_time.msecsTo(qlist.at(i).wpt->creation_time));
               if (diff_time >= max_diff_time) {
                 continue;
               }
@@ -156,13 +158,13 @@ void PositionFilter::process()
 
 void PositionFilter::init()
 {
-  char* fm;
 
   pos_dist = 0.0;
-  max_diff_time = 0.0;
+  max_diff_time = 0;
   check_time = false;
 
   if (distopt != nullptr) {
+    char* fm;
     pos_dist = strtod(distopt, &fm);
 
     if (!((*fm == 'm') || (*fm == 'M'))) {
@@ -173,7 +175,7 @@ void PositionFilter::init()
 
   if (timeopt != nullptr) {
     check_time = true;
-    max_diff_time = strtod(timeopt, &fm);
+    max_diff_time = qRound64(strtod(timeopt, nullptr) * 1000.0);
   }
 }
 
index a88348da15f968be37d7ebb114689a99d8a5aef7..0a51d98050d1df69d8bd091f05bcb0373bad9056 100644 (file)
@@ -22,7 +22,9 @@
 #ifndef POSITION_H_INCLUDED_
 #define POSITION_H_INCLUDED_
 
-#include <QVector>         // for QVector
+#include <QString>   // for QString
+#include <QVector>   // for QVector
+#include <QtGlobal>  // for qint64
 
 #include "defs.h"    // for route_head (ptr only), ARG_NOMINMAX, ARGTYPE_FLOAT
 #include "filter.h"  // for Filter
@@ -43,7 +45,7 @@ private:
   route_head* cur_rte = nullptr;
 
   double pos_dist{};
-  double max_diff_time{};
+  qint64 max_diff_time{};
   char* distopt = nullptr;
   char* timeopt = nullptr;
   char* purge_duplicates = nullptr;